<?php if (!Yii::app()->request->isAjaxRequest): ?>
    <div class="model_actions_toolbar">
        <?php $this->widget('backend.components.toolbar.MediaCategories'); ?>
    </div>
    <div>
        <?php $this->widget('backend.components.toolbar.MediaFinder', array('show_type' => 'table')); ?>
    </div>
<?php endif; ?>
<?php if (!Yii::app()->request->isAjaxRequest) echo '<div id="media_lists">'; ?>
<?php
echo CHtml::beginForm(array('media/delete'), 'post', array(
    'onSubmit' => "return fnOnSubmitDeleteConfirm('" . CHtml::encode(Yii::t('js', 'confirm_delete')) . '\',\'' . Yii::t('js', 'unselect_tips') . "');"
));
?>
<table border="0" width="100%" cellpadding="0" cellspacing="0" class="datas_grid">
    <thead>
        <tr>
            <th><input type="checkbox" id="checkallboxs_top" onclick="$('input[name=\'delete[]\']').attr('checked', this.checked);" /></th>
            <th>
                <?php echo CHtml::beginForm(); ?>
                <?php
                $has_cache = Yii::app()->cache->get('cache_media_categories_for_change');
                if ($has_cache === false) {
                    $datas = MediaCategory::model()->getCTreeViewDatas();
                    require_once(Yii::app()->basePath . '/extensions/GraphTree/tree.php');
                    $datas = dumpArrayTree($datas);
//                    $media_categories = array(
//                        '0' => 'ΞΞ All Media Files ΞΞ',
//                        '1' => 'ΞΞ NODE ROOT ΞΞ'
//                    );
                    $media_categories = array('' => 'Will Move To ......');
                    if (count($datas)) {
                        foreach ($datas as $data) {
                            $media_categories[$data['id']] = $data['levelstr'] . '⇒ ' . strip_tags($data['text']) . '《' . (string)$data['files_count'] . '》';
                        }
                    }
                    Yii::app()->cache->set('cache_media_categories_for_change', $media_categories, 300);
                } else {
                    $media_categories = Yii::app()->cache->get('cache_media_categories_for_change');
                }

                echo CHtml::dropDownList('category_id', 0, $media_categories, array('onchange' => "fnChangeCategory();")
//                array('size' => 1, 'class' => 'dropdown', 'ajax' => array(
//                        'type' => 'POST',
//                        'url' => array('changeCategory'),
//                        'data' => "ids=" . "fnGetIds()",
//                        'update' => '#media_lists',
//                        'beforeSend' => 'function() {
//                                alert(fnGetIds());
//                                $(this).data = "ids=xxxxx";
//                                $("#media_lists").addClass("datasLoading");
//                            }',
//                        'success' => 'function(result) { alert(result); $("#media_lists").removeClass("datasLoading"); }',
//                    ),
//                    'prompt' => 'Move to ......'
//                )
                        );
                ?>
                <?php
                echo CHtml::hiddenField('show_type', 'icon');
                echo CHtml::endForm();
                ?>
            </th>
            <th><?php echo $sort->link('file_name'); ?></th>
            <th><?php echo $sort->link('file_size'); ?></th>
            <th><?php echo $sort->link('file_extension'); ?></th>
            <th><?php echo $sort->link('created'); ?></th>
            <th><?php echo $sort->link('updated'); ?></th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td>
                <input type="checkbox" id="checkallboxs_bottom" onclick="$('input[name=\'delete[]\']').attr('checked', this.checked);" />
                <?php
//                echo CHtml::hiddenField('current_node', $current_node);
                echo CHtml::imageButton(Yii::app()->baseUrl . '/assets/backend/images/action_delete.png');
                ?>
            </td>
            <td colspan="7">
                <?php $this->widget('BackendPager', array('pages' => $pages,)); ?>
            </td>
        </tr>
    </tfoot>
    <tbody>
        <?php foreach($models as $n => $model): ?>
        <tr class="<?php echo ($n % 2) ? 'even' : 'odd'; ?>">
            <td class="sn_and_checkbox">
                <input type="checkbox" name="delete[]" value="<?php echo $model->id; ?>">
                <?php echo sprintf("%02d", ++$n + $pages->getCurrentPage() * $pages->getPagesize()); ?>
            </td>
            <td id="category_name_<?php echo $model->id; ?>"><?php echo CHtml::encode($model->category->name); ?></td>
            <td><?php echo CHtml::encode($model->file_name); ?></td>
            <td><?php echo $model->fileSizeFormatKB; ?></td>
            <td><?php echo strtoupper($model->file_extension); ?></td>
            <td class="date"><?php echo date('Y-m-d', $model->created); ?></td>
            <td class="date"><?php echo $model->updatedFormatted; ?></td>
            <td class="action">
                <?php
                echo CHtml::link(CHtml::image(Yii::app()->baseUrl . '/assets/backend/images/btn_edit.png'),
                    array('update', 'id' => $model->id)
                    );
                ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php echo CHtml::endForm(); ?>
<?php if (!Yii::app()->request->isAjaxRequest) echo '</div>'; ?>